package org.exam.system.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.exam.system.domain.Logininfo;
import org.exam.system.query.LogininfoQuery;
import org.exam.system.service.ILogininfoService;
import org.exam.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
@Api(tags = "用户提示")
public class LogininfoController {
    @ApiOperation(value = "查询单个接口")
    @PutMapping
    public JsonResult add(@RequestBody Logininfo logininfo) {
        try {
            if (logininfo.getId() == null) {
                iLogininfoService.add(logininfo);
                return JsonResult.success();
            } else {
                iLogininfoService.update(logininfo);
                return JsonResult.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();

        }
    }

    @Autowired
    private ILogininfoService iLogininfoService;

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable Long id) {
        try {
            iLogininfoService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @GetMapping("/{id}")
    public Logininfo get(@PathVariable("id") Long id) {
        return iLogininfoService.get(id);
    }

    @GetMapping
    public List<Logininfo> getAll() {
        return iLogininfoService.getAll();
    }

    @PatchMapping
    public JsonResult patchDelete(@RequestBody Long[] ids) {
        try {
            iLogininfoService.patchDelete(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping
    public PageInfo<Logininfo> queryPage(@RequestBody LogininfoQuery logininfoQuery) {
        return iLogininfoService.queryPage(logininfoQuery);
    }
}
